package br.com.sigmah.controller.mbeans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;
import org.primefaces.event.FlowEvent;

import br.com.sigmah.controller.facade.GradeHorariaFacade;
import br.com.sigmah.controller.facade.UtilsFacade;
import br.com.sigmah.controller.utils.GradeHorariaVO;
import br.com.sigmah.controller.utils.HoraVO;

@ManagedBean
@SessionScoped
public class GradeHorariaController{

    private static Logger logger = Logger.getLogger(GradeHorariaController.class.getName());  

	public GradeHorariaController(){
		
	}
	
	private GradeHorariaVO gradeHorariaVO;
	
	private HoraVO horaVOAux;
	
	private List<HoraVO> listaHora;
	
	private List<SelectItem> listaDeHoras;
	
	private List<SelectItem> listaDeMinutos;
	
	private String hora;
	private String minuto;

	
	@ManagedProperty("#{utilsFacade}")
	private UtilsFacade utilsFacade;
	
	@ManagedProperty("#{gradeFacade}")
	private GradeHorariaFacade gradeHorariaFacade;
	
	
	@PostConstruct
	public void init(){
		this.gradeHorariaVO = new GradeHorariaVO();
		this.listaHora = new ArrayList<HoraVO>();
		processaComboHora();
		processaComboMinuto();
	}
	

	public String salvar(){
		
		gradeHorariaFacade.salvar(gradeHorariaVO, listaHora);
		return "home";
	}
	
	public void buscar(){
		System.out.println("Buscando");
	
	}
	
	public void addHorario(){
		listaHora.add(new HoraVO(hora, minuto));
	}
	
	public void removerHorario(){
		if(listaHora.contains(horaVOAux)){
			listaHora.remove(horaVOAux);
		}
		
	}
	
	public void excluir(){
	}
	
	public void atualizar(){
	}
	
	public void onProcess(){

	}
	
	public String onFlowProcess(FlowEvent event) {  
        logger.info("Current wizard step:" + event.getOldStep());  
        logger.info("Next step:" + event.getNewStep());  
      
        return event.getNewStep();  
  
    }

	
	public void processaComboHora(){	
		if(listaDeHoras == null){
			List<SelectItem> lista = new ArrayList<SelectItem>();
			for(int i=0;i<24;i++){
				StringBuilder hora = new StringBuilder();
				if(i<10){
					hora.append("0");
				}
				hora.append(i);
				lista.add(new SelectItem(hora.toString(),hora.toString()));
			}
			listaDeHoras = lista;
		}
	}

	public void processaComboMinuto(){
		if(this.listaDeMinutos==null){
			listaDeMinutos = new ArrayList<SelectItem>();
			listaDeMinutos.add(new SelectItem("00", "00"));
			listaDeMinutos.add(new SelectItem("05", "05"));
			listaDeMinutos.add(new SelectItem("10", "10"));
			listaDeMinutos.add(new SelectItem("15", "15"));
			listaDeMinutos.add(new SelectItem("20", "20"));
			listaDeMinutos.add(new SelectItem("25", "25"));
			listaDeMinutos.add(new SelectItem("30", "00"));
			listaDeMinutos.add(new SelectItem("35", "35"));
			listaDeMinutos.add(new SelectItem("40", "00"));
			listaDeMinutos.add(new SelectItem("45", "45"));
			listaDeMinutos.add(new SelectItem("50", "00"));
			listaDeMinutos.add(new SelectItem("55", "55"));

		}
	}
	
	public UtilsFacade getUtilsFacade() {
		return utilsFacade;
	}


	public void setUtilsFacade(UtilsFacade utilsFacade) {
		this.utilsFacade = utilsFacade;
	}

	public GradeHorariaVO getGradeHorariaVO() {
		return gradeHorariaVO;
	}


	public void setGradeHorariaVO(GradeHorariaVO gradeHorariaVO) {
		this.gradeHorariaVO = gradeHorariaVO;
	}


	public HoraVO getHoraVOAux() {
		return horaVOAux;
	}


	public void setHoraVOAux(HoraVO horaVOAux) {
		this.horaVOAux = horaVOAux;
	}


	public List<HoraVO> getListaHora() {
		return listaHora;
	}


	public void setListaHora(List<HoraVO> listaHora) {
		this.listaHora = listaHora;
	}


	public List<SelectItem> getListaDeHoras() {
		return listaDeHoras;
	}


	public void setListaDeHoras(List<SelectItem> listaDeHoras) {
		this.listaDeHoras = listaDeHoras;
	}


	public List<SelectItem> getListaDeMinutos() {
		return listaDeMinutos;
	}


	public void setListaDeMinutos(List<SelectItem> listaDeMinutos) {
		this.listaDeMinutos = listaDeMinutos;
	}


	public String getHora() {
		return hora;
	}


	public void setHora(String hora) {
		this.hora = hora;
	}


	public String getMinuto() {
		return minuto;
	}


	public void setMinuto(String minuto) {
		this.minuto = minuto;
	}


	public GradeHorariaFacade getGradeHorariaFacade() {
		return gradeHorariaFacade;
	}


	public void setGradeHorariaFacade(GradeHorariaFacade gradeHorariaFacade) {
		this.gradeHorariaFacade = gradeHorariaFacade;
	}




	
}
